package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.common.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.common.converter.StringToItemTypeConverter;
import com.atguigu.lease.web.admin.custom.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: shenhao
 * @CreatTime: 2024/9/5 16:05
 * @Version: 1.0
 */
//@Configuration
@SpringBootConfiguration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private StringToItemTypeConverter stringToItemTypeConverter;
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;
    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    @Value("${admin.auth.path-patterns.include}")
    private String[] includePathPatterns;
    @Value("${admin.auth.path-patterns.exclude}")
    private String[] excludePathPatterns;


    //最终采用的是ConverterFactory方案，因此StringToItemTypeConverter相关代码可以直接删除。
    @Override
    public void addFormatters(FormatterRegistry registry) {
        //registry.addConverter(this.stringToItemTypeConverter);
        //注册转换工厂
        registry.addConverterFactory(this.stringToBaseEnumConverterFactory);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.authenticationInterceptor)
                .addPathPatterns(this.includePathPatterns)
                .excludePathPatterns(this.excludePathPatterns);
    }
}
